package com.xqls.biz.impl;

import com.xqls.biz.AdministrationBiz;
import com.xqls.biz.DischargeRealdataRecordBiz;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisKeyEnum;
import com.xqls.service.DischargeRealdataRecordService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hdg on 2017/7/14.
 */
@Service
public class DischargeRealdataRecordBizImpl implements DischargeRealdataRecordBiz {
    private static Logger logger = Logger.getLogger(DischargeRealdataRecordBiz.class);
    @Autowired
    private AdministrationBiz administrationBiz;
    @Autowired
    private DischargeRealdataRecordService DischargeRealdataRecordService;
    @Override
    public CommonResult queryDischargeRealdataRecord(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult commonResult = new CommonResult();
        try {
            List<String> columns_name = new ArrayList<String>();

            List<Object> columns = new ArrayList<Object>();
            columns.add("viewId");
            List<String> displayfiled = administrationBiz.conversion("discharge_realdata");
            for (String display : displayfiled) {
                if(!"device_no".equals(display)){
                    columns.add(display);

                }
            }
            columns.add("recording_time");
            List<String> fieldalias = administrationBiz.fieldalias("discharge_realdata");
            for (String fieldalia : fieldalias) {
                columns_name.add(fieldalia);
            }
            columns_name.add("记录时间");
            List<Map<String, Object>> datas = DischargeRealdataRecordService.selectByMap(commonPage.pageToMap());
            int curr=commonPage.getCurr();
            for (int i = 0;i<datas.size();i++){
                Date time = (Date) datas.get(i).get("recording_time");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(time);
                datas.get(i).put("recording_time",dateString);
                datas.get(i).put("viewId", i + 1+((curr-1)*15));
            }
            commonPage.setPageData(datas);
            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("realdatarecordList", commonPage.getPageData());
            showMap.put("columns", CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
                    columns.toArray(), null, "排放口实时数据历史记录"));

            commonPage.setHtml(VelocityUtil.getHtmlByMap("RealdataRecord/realdatarecordListData.vm", showMap,request,response,request.getSession().getServletContext()));
            commonResult.setIsSuccess(true);
        } catch (Exception e) {
            logger.error("排放口实时数据历史记录查询异常", e);
        }
        return commonResult;
    }

    @Override
    public List<Map<String, Object>> queryAll() {
        return DischargeRealdataRecordService.selectAll();
    }
}
